<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item> 权限管理 </el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!--  -->
      <el-table :data="rolelist" border>
        <!--展开列  -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row v-for="(item, i) in scope.row.children" :key="item.id" :class="['bdbottom', i === 0 ? 'bdtop' : '', 'venter']">
              <!-- 1 -->
              <el-col :span="5">
                <el-tag type="primary" closable @close="removeRightbyid(scope.row, item.id)"> {{ item.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 2,3 -->
              <el-col :span="19">
                <el-row :class="[i2 === 0 ? '' : 'bdtop', 'venter']" v-for="(item2, i2) in item.children" :key="item2.id">
                  <el-col :span="6">
                    <el-tag type="success" closable @close="removeRightbyid(scope.row, item2.id)">{{ item2.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三 -->
                  <el-col :span="18">
                    <el-tag closable @close="removeRightbyid(scope.row, item3.id)" type="warning" v-for="item3 in item2.children" :key="item3.id">{{ item3.authName }}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
            <!-- <pre>{{ scope.row }}</pre> -->
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色列表" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-search"> 编辑 </el-button>
            <el-button size="mini" type="denger" icon="el-icon-delete"> 删除 </el-button>
            <el-button size="mini" type="warning" icon="el-icon-setting" @click="showSetDialog(scope.row)"> 分配权限 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分配权限 对话框-->
    <el-dialog title="分配权限" :visible.sync="setrighDialogVisible" width="30%" @close="setRightDiaClo">
      <!--  props-->
      <el-tree :data="reighthslist" :props="treeProps" show-checkbox node-key="id" default-expand-all :default-checked-keys="deFKxx" ref="treeRef"></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setrighDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allrighthts">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        rolelist: [],
        setrighDialogVisible: false,
        reighthslist: [],
        // 树形控件
        treeProps: {
          // 使用那个节点嵌套
          children: 'children',
          // 每个名字
          label: 'authName'
        },
        deFKxx: [],
        // 角色id
        roleId: ''
      }
    },
    created() {
      this.getRolesList()
    },
    methods: {
      async getRolesList() {
        const { data: res } = await this.$http.get('roles')

        // 判断是否失败
        if (res.meta.status !== 200) {
          return this.$message.error('获取用户失败')
        }
        // 成功数据保存给data
        this.rolelist = res.data
        //(this.rolelist)
      },
      async removeRightbyid(role) {
        const consult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch((err) => err)(consult)
        if (consult !== 'confirm') {
          return this.$message.info('取消了删除')
        }
        ;('确认删除')

        const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${ididx}`)
        if (res.meta.status !== 200) {
          return this.$message.error('删除失败')
        }
        role.children = res.data
      },
      async showSetDialog(role) {
        this.roleId = role.id(role.id)
        const { data: res } = await this.$http.get('rights/tree')
        if (res.meta.status !== 200) {
          return this.$message.error('获取权限失败')
        }
        // 获取的权限数据保存到
        this.reighthslist = res.data
        //(this.reighthslist)
        //递归
        this.getLeafKeys(role, this.deFKxx)
        this.setrighDialogVisible = true
      },
      getLeafKeys(node, arr) {
        if (!node.children) {
          return arr.push(node.id)
        }
        node.children.forEach((item) => {
          this.getLeafKeys(item, arr)
        })
      },
      // 监听分配权限对话框
      setRightDiaClo() {
        this.deFKxx = []
      },
      async allrighthts() {
        const keys = [...this.$refs.treeRef.getCheckedKeys(), ...this.$refs.treeRef.getHalfCheckedKeys()](keys)
        const idStr = keys.join(',')
        // 请求体
        const { data: res } = await this.$http.post(`roles/${this.roleId}/rights`, {
          rids: idStr
        })
        if (res.meta.status !== 200) {
          return this.$message.error('分配权限失败')
        }
        this.$message.success('分配权限成功')
        this.getRolesList()
        this.setrighDialogVisible = false
      }
    }
  }
</script>

<style lang="less" scoped>
  .el-tag {
    margin: 7px;
  }
  .bdtop {
    border-top: 1px solid #eee;
  }
  .bdbottom {
    border-bottom: 1px solid #eee;
  }
  //
  .venter {
    display: flex;
    align-items: center;
  }
</style>
